package net.acoder.dz.transmit;

import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

public class ServerTest {
    public static void main(String[] args) throws IOException {
        startServer("ivr", 23456);
        startServer("server", 34567);
    }

    static void startServer(final String name, int port) throws IOException {
        final ServerSocket server = new ServerSocket(port);
        new Thread(new Runnable() {
            public void run() {
                while (true) {
                    try {
                        final Socket socket = server.accept();
                        System.out.println("new connection to " + name);
                        new Thread(new Runnable() {
                            public void run() {
                                DataOutputStream dos = null;
                                try {
                                    dos = new DataOutputStream(socket
                                            .getOutputStream());
                                } catch (IOException e1) {
                                    e1.printStackTrace();
                                }
                                try {
                                    while (true) {
                                        String str = name
                                                + System.currentTimeMillis();
                                        System.out.println(str);
                                        dos.writeUTF(str);
                                        Thread.sleep(500);
                                    }
                                } catch (Exception e) {
                                    e.printStackTrace();
                                }
                            }
                        }).start();
                    } catch (IOException e1) {
                    }
                }
            }
        }).start();
    }
}
